package nc.bs.hdysc.dao;

import java.util.List;

import nc.bs.dao.BaseDAO;
import nc.itf.hdysc.web.IWebData;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;

public class WebDataDao implements IWebData {

	private String DS = "web";

	private BaseDAO dao;

	private BaseDAO getDao() {
		if (dao == null) {
			dao = new BaseDAO(DS);
		}

		return dao;
	}

	public WebDataDao() {
	};

	public WebDataDao(String ds) {
		this.DS = ds;
	}

	public <T extends SuperVO> List<T> qryBySql(String sql, Class<T> c) throws BusinessException {
		return (List<T>) getDao().executeQuery(sql, new BeanListProcessor(c));
	}

	public <T extends SuperVO> List<T> qryByCondition(String condition, Class<T> c) throws BusinessException {
		return (List<T>) getDao().retrieveByClause(c, condition);
	}

	@SuppressWarnings("unchecked")
	public <T extends SuperVO> T qryByPk(String pk, Class<T> c) throws BusinessException {
		return (T) getDao().retrieveByPK(c, pk);
	}

	public <T extends SuperVO> void delete(T t) throws BusinessException {
		getDao().deleteVO(t);
	}

	public <T extends SuperVO> void save(T t) throws BusinessException {
		getDao().insertVOWithPK(t);
	}

	public <T extends SuperVO> void update(T t) throws BusinessException {
		getDao().updateVO(t);
	}

	public <T extends SuperVO> void insert(T t) throws BusinessException {
		getDao().insertVO(t);
	}

	public <T extends SuperVO> void insertOrUpdate(T t) throws BusinessException {
		Object o = getDao().retrieveByPK(t.getClass(), t.getPrimaryKey());
		if (o == null) {
			getDao().insertVOWithPK(t);
		} else {
			getDao().updateVO(t);
		}
	}

	public void update(String sql) throws BusinessException {
		getDao().executeUpdate(sql);
	}

	// public void

}
